/**
 * 文件级注释：前端主路由配置
 * 说明：定义应用的路由表与鉴权逻辑。本次变更移除了 /payments 路由，配合 UI 删除“支付导览”页面。
 * 返回值：导出已配置的 Vue Router 实例。
 * 异常：无显式抛出；鉴权失败时通过跳转实现处理。
 */
import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '../stores/auth'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    component: () => import('../layouts/AppShell.vue'),
    children: [
      { path: '', redirect: '/dashboard' },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'coupons',
        name: 'Coupons',
        component: () => import('../views/Coupons.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'coupons/detail/:id',
        name: 'CouponDetail',
        component: () => import('../views/CouponDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'coupons/edit/:id?',
        name: 'CouponEdit',
        component: () => import('../views/CouponEdit.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'coupon-packages',
        name: 'CouponPackages',
        component: () => import('../views/CouponPackages.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'coupon-packages/detail/:id',
        name: 'CouponPackageDetail',
        component: () => import('../views/CouponPackageDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'coupon-packages/edit/:id?',
        name: 'CouponPackageEdit',
        component: () => import('../views/CouponPackageEdit.vue'),
        meta: { requiresAuth: true }
      },
      // 已删除：支付导览页面 /payments
      {
        path: 'logs',
        name: 'LogManagement',
        component: () => import('../views/LogManagement.vue'),
        meta: { requiresAuth: true }
      }
      ,
      {
        path: 'merchants',
        name: 'Merchants',
        component: () => import('../views/Merchants.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('../views/Users.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'roles',
        name: 'Roles',
        component: () => import('../views/Roles.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'users/detail/:id',
        name: 'UserDetail',
        component: () => import('../views/UserDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'merchants/detail/:id',
        name: 'MerchantDetail',
        component: () => import('../views/MerchantDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'stores',
        name: 'Stores',
        component: () => import('../views/Stores.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'stores/detail/:id',
        name: 'StoreDetail',
        component: () => import('../views/StoreDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'payment-codes',
        name: 'PaymentCodes',
        component: () => import('../views/PaymentCodes.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'payment-platforms',
        name: 'PaymentPlatforms',
        component: () => import('../views/PaymentPlatforms.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'payment-codes/detail/:id',
        name: 'PaymentCodeDetail',
        component: () => import('../views/PaymentCodeDetail.vue'),
        meta: { requiresAuth: true }
      }
      ,
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('../views/Orders.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'orders/detail/:id',
        name: 'OrderDetail',
        component: () => import('../views/OrderDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'divided-agreements',
        name: 'DividedAgreements',
        component: () => import('../views/DividedAgreements.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'my-divided-agreements',
        name: 'MyDividedAgreements',
        component: () => import('../views/MyDividedAgreements.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'divided-agreements/detail/:id',
        name: 'DividedAgreementDetail',
        component: () => import('../views/DividedAgreementDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'divided-records',
        name: 'DividedRecords',
        component: () => import('../views/DividedRecords.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'divided-records/detail/:id',
        name: 'DividedRecordDetail',
        component: () => import('../views/DividedRecordDetail.vue'),
        meta: { requiresAuth: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

/**
 * 函数级注释：全局路由守卫
 * 逻辑：
 * 1. 对带有 `requiresAuth` 的路由进行鉴权（必须已登录）。
 * 2. 限制非“平台管理员/平台超级管理员”的用户访问受保护页面。
 * 参数：
 *  - to 目标路由
 *  - _from 来源路由
 *  - next 路由放行函数
 * 返回值：无（通过 next 控制导航）
 * 异常：无显式抛出，统一通过重定向处理
 */
router.beforeEach((to, _from, next) => {
  const auth = useAuthStore()
  // 登录页直接放行
  if (to.path === '/login') {
    next()
    return
  }
  // 需要登录的页面统一鉴权
  if (to.meta.requiresAuth) {
    if (!auth.isAuthenticated) {
      next({ path: '/login', query: { redirect: to.fullPath } })
      return
    }
    // 平台角色限制：仅平台管理员/平台超级管理员允许进入
    if (!(auth.isPlatformAdmin || auth.isPlatformSuperAdmin)) {
      next({ path: '/login', query: { redirect: to.fullPath, reason: 'no_platform_perm' } })
      return
    }
  }
  next()
})

export default router
